package com.jl15988.simpletools.system.controller;

import com.jl15988.simpletools.common.annotation.Log;
import com.jl15988.simpletools.common.constant.OperationType;
import com.jl15988.simpletools.common.utils.Result;
import com.jl15988.simpletools.system.domain.dto.UserLoginDTO;
import com.jl15988.simpletools.system.domain.dto.UserRegisterDTO;
import com.jl15988.simpletools.system.domain.vo.LoginResult;
import com.jl15988.simpletools.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 认证控制器
 *
 * @author jl15988
 */
@Slf4j
@Api(tags = "认证接口")
@RestController
@RequestMapping("/api/v1/auth")
public class AuthController {

    @Autowired
    private UserService userService;

    @Log(title = "用户登录", operationType = OperationType.LOGIN)
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<LoginResult> login(@Validated @RequestBody UserLoginDTO loginDTO) {
        log.info("用户登录: {}", loginDTO.getUsername());
        return Result.success(userService.login(loginDTO));
    }

    @Log(title = "用户注册", operationType = OperationType.REGISTER)
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<String> register(@Validated @RequestBody UserRegisterDTO registerDTO) {
        log.info("用户注册: {}", registerDTO.getUsername());
        boolean success = userService.register(registerDTO);
        return success ? Result.success("注册成功") : Result.error("注册失败");
    }
} 